package cn.tedu.loding.controller;

import cn.tedu.loding.pojo.dto.AttributeAddNewDTO;
import cn.tedu.loding.pojo.dto.AttributeTemplateAddNewDTO;
import cn.tedu.loding.pojo.dto.AttributeTemplateUpdateDTO;
import cn.tedu.loding.pojo.entity.AttributeTemplate;
import cn.tedu.loding.pojo.vo.AttributeListVO;
import cn.tedu.loding.pojo.vo.AttributeTemplateListVO;
import cn.tedu.loding.pojo.vo.AttributeTemplateStandardVO;
import cn.tedu.loding.service.IAttributeService;
import cn.tedu.loding.service.IAttributeTemplateService;
import cn.tedu.loding.web.JsonResult;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "03.属性模板管理")
@Slf4j
@RestController
@Validated
@RequestMapping("/attributeTemplates")
public class AttributeTemplateController {
    @Autowired
    IAttributeTemplateService service;

    @Autowired
    IAttributeService attributeService;

    //localhost:9090/attributeTemplates
    @ApiOperation("分页查询的属性模板列表")
    @ApiOperationSupport(order = 440)
    @GetMapping("/{page}/page/{pageSize}/pageSize")
    public JsonResult<PageInfo<AttributeTemplateListVO>> attributeTemplatelist(@PathVariable Integer page, @PathVariable Integer pageSize){
        log.debug("开始处理【根据属性模板id分页查询到的属性列表】请求,参数为{},{}",page,pageSize);
        PageInfo<AttributeTemplateListVO>pageInfo =
                service.getAllAttributeTemplateByPage(page,pageSize);

        return JsonResult.ok(pageInfo);
    }


    //localhost:9090/attributeTemplates/add-new
    @ApiOperation("添加属性模板")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Valid AttributeTemplateAddNewDTO attributeTemplateAddNewDTO){
        log.debug("开始处理【添加属性模板】请求,参数为{}",attributeTemplateAddNewDTO);
        service.addNew(attributeTemplateAddNewDTO);
        String message = "添加成功!";
        log.debug(message);
        return JsonResult.ok();
    }

    //localhost:9090/attributeTemplates/9527/delete
    @ApiOperation("删除属性模板")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id",value = "属性模板",required = true,type = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1,message = "尝试删除的属性模板ID") @PathVariable Long id){
        log.debug("开始处理【删除属性模板】请求,参数为{}",id);
        service.delete(id);
        String message = "删除成功!";
        log.debug(message);
        return JsonResult.ok();
    }

    // http://localhost:9090/attributeTemplates/9527/update
    @ApiOperation("根据id修改属性模板")
    @ApiOperationSupport(order = 300)
    @PostMapping("/update")
    public JsonResult<Void> update(@Valid AttributeTemplateUpdateDTO attributeTemplateUpdateDTO) {
        String message = "尝试修改id为【" + attributeTemplateUpdateDTO.getId() + "】的类别";
        log.debug(message);
        service.update(attributeTemplateUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9090/attributeTemplates/9527/getStandardById
    @ApiOperation("根据id查询属性模板详情")
    @ApiOperationSupport(order = 410)
    @GetMapping("/{id:[0-9]+}/getStandardById")
    public JsonResult<AttributeTemplateStandardVO> getStandardById(@PathVariable Long id){
        String message = "尝试根据id为【"+ id +"】查询属性模板详情";
        log.debug(message);
        AttributeTemplateStandardVO attributeTemplateStandardVO = service.getStandardById(id);
        return JsonResult.ok(attributeTemplateStandardVO);
    }




}
